package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.RecommendUser;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;


    //根据登录用户id，查询缘分值最高(Recomment_user表中score值最大)的用户
    @Override
    public RecommendUser findTodayBest(Long userId) {

        //注意：这里toUserId才是登录用户的id
        Query query = new Query(Criteria.where("toUserId").is(userId)).with(Sort.by(Sort.Order.desc("score"))).skip(0).limit(1);

        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        return recommendUser;
    }

    //根据登录用户id分页获取其推荐用户列表
    @Override
    public PageBeanVo findRecommendUserList(Long userId, Integer pageNum, Integer pageSize) {

        //条件,根据登录用户id，按推荐分数倒叙查询推荐用户集合
        Query query = new Query(Criteria.where("toUserId").is(userId)).with(Sort.by(Sort.Order.desc("score"))).skip((pageNum - 1) * pageSize + 1).limit(pageSize);

        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);

        //记录条数
        long count = mongoTemplate.count(query, RecommendUser.class);

        //封账一个pageBeanVo
        return new PageBeanVo(pageNum,pageSize,count,recommendUserList);
    }

    //根据两人id调用service查询推荐用户信息
    @Override
    public RecommendUser findRecommendUser(Long userId, Long recommendId) {
        //条件
        Query query = new Query(Criteria.where("toUserId").is(userId).and("userId").is(recommendId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }
}
